﻿using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace Baggr.ClientSite.Models
{
    using System;
    [TypeConverter(typeof(FeedItemsFilterConverter)), Serializable]
    public class FeedItemsFilter
        : IEquatable<FeedItemsFilter>
    {
        private enum Values
        {
            ShowAll = 0,
            ShowUnreaded = 1
        }
        [NonSerialized]
        private static readonly FeedItemsFilter _ShowAll = new FeedItemsFilter(Values.ShowAll);
        [NonSerialized]
        private static readonly FeedItemsFilter _ShowUnreaded = new FeedItemsFilter(Values.ShowUnreaded);

        private readonly Values _Value;
        private FeedItemsFilter(Values value)
        {
            if (!Enum.IsDefined(typeof(Values), value))
                throw new ArgumentException("Value doesn't exist", "value");
            _Value = value;
        }
        public static FeedItemsFilter ShowAll
        {
            get
            {
                return _ShowAll;
            }
        }
        public static FeedItemsFilter ShowUnreaded
        {
            get
            {
                return _ShowUnreaded;
            }
        }

        public string Text
        {
            get
            {
                if (IsShowAll)
                    return "All";
                if (IsShowUnreaded)
                    return "Unreaded";
                throw new InvalidDataException("Это невозможно");
            }
        }

        public string AsString
        {
            get
            {
                return ToString();
            }
        }

        public static FeedItemsFilter Parse(string value)
        {
            switch(value.ToUpper())
            {
                case "SHOWALL":
                    return ShowAll;
                case "SHOWUNREADED":
                    return ShowUnreaded;
                default:
                    return null;
            }
        }

        public bool IsShowAll
        {
            get
            {
                return _Value == Values.ShowAll;
            }
        }

        public bool IsShowUnreaded
        {
            get
            {
                return _Value == Values.ShowUnreaded;
            }
        }

        public bool Equals(FeedItemsFilter other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;
            return _Value == other._Value;
        }

        public override string ToString()
        {
            return Enum.GetName(typeof (Values), _Value);
        }

        public override int GetHashCode()
        {
            return _Value.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj as FeedItemsFilter);
        }

        public static IEnumerable<FeedItemsFilter> All
        {
            get
            {
                yield return ShowAll;
                yield return ShowUnreaded;
            }
        }
    }
}
